﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Data.Objects;
using MetadataBasedConfigArch.Framework.Entity;
using System.Data.Metadata.Edm;
using System.Reflection;
using System.Data.Mapping;
using System.Collections.ObjectModel;

namespace MetadataBasedConfigArch.Web
{
    public partial class Login : System.Web.UI.Page
    {
        #region Page Load

        protected void Page_Load(object sender, EventArgs e)
        {


            string pathToDataFolder = @"E:\Open Source\tfs.CodePlex.com\TFS01\devinvest\MetadataBasedConfigArch\MetadataBasedConfigArch.Model\Model1.edmx";
            MetadataWorkspace metdataWorkspace = new MetadataWorkspace();
            EdmItemCollection edmItemCollection = new EdmItemCollection(pathToDataFolder);
            metdataWorkspace.RegisterItemCollection(edmItemCollection);

            StoreItemCollection storeItemCollection = new StoreItemCollection(pathToDataFolder);
            metdataWorkspace.RegisterItemCollection(storeItemCollection);

            StorageMappingItemCollection storageMappingItemCollection = new StorageMappingItemCollection(edmItemCollection, storeItemCollection, pathToDataFolder);
            metdataWorkspace.RegisterItemCollection(storageMappingItemCollection);

            GetItemsFromModel(metdataWorkspace, DataSpace.OSpace);

            GetItemsFromModel(metdataWorkspace, DataSpace.SSpace);
            GetItemsFromModel(metdataWorkspace, DataSpace.OCSpace);
            //System.Web.UI.WebControls.DataList dataList = new DataList();
            //dataList.DataSource = new List<string>();
            //dataList.DataBind();  
            //PropertyDescriptor propertyDescriptor = TypeDescriptor.CreateProperty(typeof(TestRole), "ID", typeof(int));
            //TestRole testRole = new TestRole();
            
            //propertyDescriptor.SetValue(testRole, 1);
            //var dd = propertyDescriptor.GetValue(testRole);
            if (!IsPostBack)
            {
                BindData();
            }
        }

        public void GetItemsFromModel(MetadataWorkspace workspace, DataSpace dataSpace)
        {
            ReadOnlyCollection<EdmType> types = workspace.GetItems<EdmType>(dataSpace);
            foreach (var type in types)
            {
                string name = type.Name;
            }
        }

        #endregion

        #region Bind Data

        protected void BindData()
        {
            var allTenants = new List<Model.Tenant>();
            using (Model.MDAEntities entities = EntityUtil.LoadEntities())
            {
                allTenants = (from t in entities.Tenants select t).ToList();
            }
            TenantsDropDownList.DataTextField = "Name";
            TenantsDropDownList.DataValueField = "ID";
            TenantsDropDownList.DataSource = allTenants;
            TenantsDropDownList.DataBind(); 
        }

        #endregion

        #region Events

        protected void LoginButton_Click(object sender, EventArgs e)
        {
            var selectedTenantID = TenantsDropDownList.SelectedIndex >= 0 ? Convert.ToInt64(TenantsDropDownList.SelectedValue) : 0;
            if (selectedTenantID > 0)
            {
                using (Model.MDAEntities entities = EntityUtil.LoadEntities())
                {
                    var user = (from u in entities.Users
                                where u.Name == UserNameTextBox.Text.Trim()
                                    && u.Password == PasswordTextBox.Text.Trim()
                                    && u.TenantID == selectedTenantID 
                                select u).FirstOrDefault();
                    if (user != null)
                    {
                        Util.TenantID = user.TenantID;
                        Util.UserID = user.ID;
                        Dictionary<string, object> staticConstraints = new Dictionary<string, object>();
                        staticConstraints.Add("TenantID", user.TenantID);
                        StaticConstraintHelper .InitializeStaticConstraints(staticConstraints);
                        Response.Redirect(String.IsNullOrEmpty(Request["RedirectUrl"])?"~/Admin/Default.aspx":Request["RedirectUrl"]);
                    }
                }
            }
        }

        protected void CancelButton_Click(object sender, EventArgs e)
        {

        }

        #endregion

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        { 
        }
    }

    public class TestRole
    {
        public string Name { get; set; }
    }
}